//
//  UICollectionViews.swift
//  JXBaseTools
//
//  Created by godox on 2021/11/30.
//

import UIKit


extension JX where Base: UICollectionView {
    
    public func registerCell<T: UICollectionViewCell>(_ type: T.Type) {
        let identifier = String(describing: type.self)
        base.register(type, forCellWithReuseIdentifier: identifier)
    }
    
    public func registerXibCell<T: UICollectionViewCell>(_ type: T.Type) {
        let identifier = String(describing: type.self)
        base.register(UINib(nibName: "\(type)", bundle: nil), forCellWithReuseIdentifier: identifier)
    }
    
    public func setDelegateAndDataSource(_ delegage: UICollectionViewDelegate, _ dataSource: UICollectionViewDataSource){
        base.delegate = delegage
        base.dataSource = dataSource
    }
    
    public func dequeueReusableCell<T: UICollectionViewCell>(_ type: T.Type, for indexPath: IndexPath) -> T {
        let identifier = String(describing: type.self)
        guard let cell = base.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as? T else
        {
            fatalError("\(type.self) was not registered")
        }
        return cell
    }
}
